package cn.juque.jdfsystem.listener;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ClassUtil;
import cn.juque.common.constants.YesOrNoEnum;
import cn.juque.jdfsystem.entity.ConfigInfo;
import cn.juque.lucenecandy.bo.IndexUpdateParamBO;
import cn.juque.lucenecandy.core.datasync.listener.ICommitListener;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author juque
 * @version 1.0.0
 * <ul>
 *     <li>过滤可以同步到其他实例的配置</li>
 * </ul>
 * @date 2024-01-05
 **/
@Service("configCheckSyncFlagListener")
public class ConfigCheckSyncFlagListener implements ICommitListener {

    @Override
    public Boolean before(IndexUpdateParamBO param) {
        if (!ClassUtil.equals(ConfigInfo.class, param.getClassName(), false)) {
            return true;
        }
        if (CollUtil.isEmpty(param.getContent())) {
            return false;
        }
        List<ConfigInfo> list = (List<ConfigInfo>) param.getContent();
        list = list.stream().filter(f -> YesOrNoEnum.YES.getCode().equals(f.getSyncFlag())).collect(Collectors.toList());
        if (CollUtil.isEmpty(list)) {
            return false;
        }
        param.setContent(list);
        return true;
    }

    @Override
    public void after(IndexUpdateParamBO param) {
        // do nothing
    }
}
